package com.xusheng.cli.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xusheng.cli.common.base.MyServiceImpl;
import com.xusheng.cli.entity.backAgentModule.BackAgent;
import com.xusheng.cli.entity.backAgentModule.BackAgentUpdatePsd;
import com.xusheng.cli.mapper.BackAgentMapper;
import com.xusheng.cli.mapper.ProxyHostMapper;
import com.xusheng.cli.mapper.ProxyHostThreeMapper;
import com.xusheng.cli.mapper.ProxyHostTwoMapper;
import com.xusheng.cli.service.BackAgentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代理商信息表(BackAgent)表服务实现类
 *
 * @author whr
 * @since 2024-03-19 17:56:47
 */
@Service("backAgentService")
public class BackAgentServiceImpl extends MyServiceImpl<BackAgentMapper, BackAgent> implements BackAgentService {

    @Resource
    private ProxyHostMapper proxyHostMapper;
    @Resource
    private ProxyHostTwoMapper proxyHostTwoMapper;
    @Resource
    private ProxyHostThreeMapper proxyHostThreeMapper;

    /**
     * 修改密码
     *
     * @param backAgent
     * @return
     */
    @Override
    public boolean updatePassword(BackAgentUpdatePsd backAgent) {
        //判断二级密码
        BackAgent backAgent1 = baseMapper.selectById(backAgent.getId());
        // 判断原密码
//        boolean b = DigestUtil.bcryptCheck(backAgent.getOldPassword(), backAgent1.getPassword());
//        if (b) {
        //更新新密码
        backAgent1.setPassword(DigestUtil.bcrypt(backAgent.getPassword()));
        return updateById(backAgent1);
//        } else {
//            throw new ServiceException("原密码错误!");
//        }
    }

    /**
     * 删除代理对应的域名
     *
     * @param backAgent
     * @return
     */
    @Override
    public void deleteHosts(BackAgent backAgent) {
        if (backAgent.getLevel() == 1) {
            Map<String, Object> hostMap = new HashMap<>();
            hostMap.put("proxy_id", backAgent.getId());
            proxyHostMapper.deleteByMap(hostMap);
            List<BackAgent> backAgent2List = baseMapper.selectList(new LambdaQueryWrapper<>(new BackAgent()).eq(BackAgent::getParentId, backAgent.getId()));
            for (BackAgent backAgent2 : backAgent2List) {
                Map<String, Object> hostMap2 = new HashMap<>();
                hostMap2.put("proxy_id", backAgent2.getId());
                proxyHostTwoMapper.deleteByMap(hostMap2);
                List<BackAgent> backAgent3List = baseMapper.selectList(new LambdaQueryWrapper<>(new BackAgent()).eq(BackAgent::getParentId, backAgent2.getId()));
                for (BackAgent backAgent3 : backAgent3List) {
                    Map<String, Object> hostMap3 = new HashMap<>();
                    hostMap3.put("proxy_id", backAgent3.getId());
                    proxyHostTwoMapper.deleteByMap(hostMap3);
                }
            }
        } else if (backAgent.getLevel() == 2) {
            Map<String, Object> hostMap2 = new HashMap<>();
            hostMap2.put("proxyId", backAgent.getId());
            proxyHostTwoMapper.deleteByMap(hostMap2);
            List<BackAgent> backAgent3List = baseMapper.selectList(new LambdaQueryWrapper<>(new BackAgent()).eq(BackAgent::getParentId, backAgent.getId()));
            for (BackAgent backAgent3 : backAgent3List) {
                Map<String, Object> hostMap3 = new HashMap<>();
                hostMap3.put("proxyId", backAgent3.getId());
                proxyHostTwoMapper.deleteByMap(hostMap3);
            }
        } else if (backAgent.getLevel() == 3) {
            Map<String, Object> hostMap3 = new HashMap<>();
            hostMap3.put("proxyId", backAgent.getId());
            proxyHostTwoMapper.deleteByMap(hostMap3);
        }
    }

}

